package com.carbon.assets.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.assets.entity.CarbonCreditAssets;
import com.carbon.assets.entity.CarbonQuotaAssets;
import com.carbon.assets.service.DictService;
import com.carbon.assets.vo.*;
import com.carbon.domain.assets.vo.MethodologyUploadParam;
import com.carbon.assets.repository.MethodologyRepository;
import com.carbon.assets.service.CarbonMethodologyService;
import com.carbon.assets.param.CarbonMethodologyQueryParam;
import com.carbon.assets.service.SynMethodologyContentService;
import com.carbon.assets.entity.CarbonMethodology;
import com.carbon.assets.common.BaseController;
import com.carbon.domain.common.ApiResult;
import com.carbon.common.api.Paging;
import com.carbon.domain.common.annotation.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 碳减排方法学 前端控制器
 * </p>
 *
 * @author Li Jun
 * @since 2021-07-31
 */
@Slf4j
@RestController
@RequestMapping("/carbonMethodology")
@Api(value = "碳减排方法学模块", tags = {"碳减排方法学模块"})
public class CarbonMethodologyController extends BaseController {

    @Resource
    CarbonMethodologyService carbonMethodologyService;
    @Resource
    DictService dictService;

    @GetMapping("/queryBySelector")
    public ApiResult queryBySelector(SelectorVo selectorVo) {
        selectorVo.setStatusCode("0450000002");
        Page<CarbonMethodology> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(
                selectorVo.getPageNum(),
                selectorVo.getPageSize()
        );
        // 3. 调用服务层的分页查询方法
        Page<CarbonMethodology> resultPage =
                carbonMethodologyService.selectBySelector(page, selectorVo);

        // 4. 处理分页结果中的状态字典转换
        for (CarbonMethodology queryVo : resultPage.getRecords()) {
            queryVo.setFieldCode(dictService.getDictCh(queryVo.getFieldCode()));
            queryVo.setIndustryCode(dictService.getDictCh(queryVo.getIndustryCode()));
            queryVo.setCertificationCriteria(dictService.getDictCh(queryVo.getCertificationCriteria()));
        }
        // 5. 返回包含分页信息的结果
        return ApiResult.ok(resultPage);
    }


    @GetMapping("/sysQueryBySelector")
    public ApiResult sysQueryBySelector(SelectorVo selectorVo) {

        Page<CarbonMethodology> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(
                selectorVo.getPageNum(),
                selectorVo.getPageSize()
        );
        // 3. 调用服务层的分页查询方法
        Page<CarbonMethodology> resultPage =
                carbonMethodologyService.selectBySelector(page, selectorVo);

        // 4. 处理分页结果中的状态字典转换
        for (CarbonMethodology queryVo : resultPage.getRecords()) {
            queryVo.setStatusCode(dictService.getDictCh(queryVo.getStatusCode()));
            queryVo.setFieldCode(dictService.getDictCh(queryVo.getFieldCode()));
            queryVo.setIndustryCode(dictService.getDictCh(queryVo.getIndustryCode()));
            queryVo.setCertificationCriteria(dictService.getDictCh(queryVo.getCertificationCriteria()));
        }
        // 5. 返回包含分页信息的结果
        return ApiResult.ok(resultPage);
    }

    @PostMapping("/upload")
    public ApiResult upload(@RequestBody CarbonMethodology carbonMethodology) {
        carbonMethodology.setStatusCode("0450000001");
        DictVo dictVo = new DictVo();
        dictVo.setDictCode("009");
        dictVo.setItemValue(carbonMethodology.getDictCode());
        dictVo.setItemCh(carbonMethodology.getName());
        dictService.save(dictVo);
        carbonMethodologyService.save(carbonMethodology);
        return ApiResult.ok("方法学上传成功");
    }

    @DeleteMapping("/delete")
    public ApiResult delete(@RequestParam String id) {
        CarbonMethodology cm = carbonMethodologyService.getById(id);
        carbonMethodologyService.removeById(id);
        LambdaQueryWrapper<DictVo> wr = new LambdaQueryWrapper<>();
        wr.eq(DictVo::getItemValue, cm.getDictCode());
        dictService.remove(wr);
        return ApiResult.ok("方法学删除成功");
    }

    @GetMapping("/updateStatus")
    public ApiResult updateStatus(@RequestParam String id) {
        CarbonMethodology methodology = new CarbonMethodology();
        methodology.setId(Long.parseLong(id));
        methodology.setStatusCode("0450000002");
        methodology.setStatus("已发布");
        carbonMethodologyService.updateById(methodology);
        return ApiResult.ok("方法学发布成功");
    }

    @GetMapping("/updateDownStatus")
    public ApiResult updateDownStatus(@RequestParam String id) {
        CarbonMethodology methodology = new CarbonMethodology();
        methodology.setId(Long.parseLong(id));
        methodology.setStatusCode("0450000003");
        methodology.setStatus("已下线");
        carbonMethodologyService.updateById(methodology);
        return ApiResult.ok("方法学下线");
    }

    private static final String DIGIT_WITH_MINUS_REGEX = "^-?[0-9]+$";
    @PostMapping("/update")
    public ApiResult update(@RequestBody CarbonMethodology carbonMethodology) {
        DictVo updateDict = new DictVo();
        updateDict.setItemCh(carbonMethodology.getName());
        LambdaQueryWrapper<DictVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictVo::getItemValue, carbonMethodology.getDictCode());

        boolean updateSuccess = dictService.update(updateDict, queryWrapper);

        if (!carbonMethodology.getFieldCode().matches(DIGIT_WITH_MINUS_REGEX)) {

            carbonMethodology.setFieldCode(dictService.getDictCode(carbonMethodology.getFieldCode()));
        }
        if (!carbonMethodology.getIndustryCode().matches(DIGIT_WITH_MINUS_REGEX)) {
            carbonMethodology.setIndustryCode(dictService.getDictCode(carbonMethodology.getIndustryCode()));
        }
        if (!carbonMethodology.getCertificationCriteria().matches(DIGIT_WITH_MINUS_REGEX)) {
            carbonMethodology.setCertificationCriteria(dictService.getDictCode(carbonMethodology.getCertificationCriteria()));
        }
        carbonMethodologyService.updateById(carbonMethodology);
        return ApiResult.ok("编辑成功");
    }
}

